/*
 * AS_DVBS.H
 *
 * autoscan module internal API for DVB-S application.
 *
 */
#ifndef __AS_DVBS_H__
#define __AS_DVBS_H__

#include <types.h>
#include <sys_config.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libpub/as_module.h>


enum
{
	SYM_DIST	= 128,
	FREQ_DIST	= 2,
	SYM_SHIFT	= 13,
};

enum
{
	PROGRESS_BAND1_OVER		= 99,
	PROGRESS_LEFT1_OVER		= 0,
	PROGRESS_HALF_WAY		= 49,
	PROGRESS_HALF_WAY_LEFT	= 0,
	PROGRESS_25_PERCENT		= 24,
};

enum
{
	KU_C_BAND_INTERFACE		= 8000,
};


#ifdef FE_DVBS_SUPPORT

static __inline__ BOOL dvbs_tp_duplicate_check(UINT32 freq, UINT32 sym, 
	UINT32 polar)
{
    static UINT32 last_freq=0, last_sym=0, last_polar=0;
    BOOL ret;
    
    if ((last_polar == polar)
        &&(sym-last_sym<SYM_DIST)&&(last_sym-sym<SYM_DIST)
        &&(freq-last_freq<FREQ_DIST+(last_sym>>SYM_SHIFT))
        &&(last_freq-freq<FREQ_DIST+(sym>>SYM_SHIFT)))
        ret = FALSE;
    else
        ret = TRUE;

    last_freq = freq;
    last_sym = sym;
    last_polar = polar;

    return ret;
}

static __inline__ void dvbs_tp_info2db(TP_INFO *info, T_NODE *tp)
{
    MEMSET(tp, 0, sizeof(T_NODE));
    tp->ft_type = FRONTEND_TYPE_S;
#ifndef COMBOUI	
    tp->network_id = info->s_info.onid;
    tp->t_s_id = info->s_info.tsid;
    tp->frq = info->s_info.frequency;
    tp->sym = info->s_info.symbol_rate;
    tp->pol = info->s_info.polarity;
    tp->FEC_inner = info->s_info.FEC_inner;
#else
	tp->network_id = info->combo_info.onid;
	tp->t_s_id = info->combo_info.tsid;
	tp->frq = info->combo_info.frequency;
	tp->sym = info->combo_info.symbol_rate;
	tp->pol = info->combo_info.polarity;
	tp->FEC_inner = info->combo_info.FEC_inner;
#endif
}

static __inline__ void dvbs_convert_freq(struct as_module_info *info,
	T_NODE *tp, UINT32 freq)
{
    UINT32 lnb_high = info->antenna.lnb_high;
    UINT32 lnb_low = info->antenna.lnb_low;
    UINT32 lnb_ctrl = info->antenna.lnb_type;

	if ((info->dual_LNB)&&(lnb_ctrl!=LNB_CTRL_22K)&&(lnb_ctrl!=LNB_CTRL_UNICABLE)/*Unicable*/)      
        tp->frq=(tp->pol=(freq>1550))? lnb_high:lnb_low;
    else
    {
        tp->pol = info->polar;
		if (lnb_ctrl != LNB_CTRL_22K && lnb_ctrl != LNB_CTRL_UNICABLE/*Unicable*/)         
            tp->frq = lnb_high>0? lnb_high:lnb_low;
        else
            tp->frq = ((lnb_low<lnb_high)^(info->antenna.k22))?lnb_low : lnb_high;
    }

	if (tp->frq>KU_C_BAND_INTERFACE) 
    {	/* Ku-band*/
		/* for Unicable, the Fub is inverted,
		   so should autoscan from Hi freq to Low freq
		   like C-band */
		if (lnb_ctrl == LNB_CTRL_UNICABLE)		
			tp->frq+=(info->nim_para.sfreq+info->nim_para.efreq-freq);		
		else
			tp->frq+=freq;
	} 
    else/* C-band */
        tp->frq-=freq;
  
}


#ifdef __cplusplus
extern "C"{
#endif
BOOL dvbs_tp_sanity_check(BOOL voltage_ctrl, T_NODE *tp, S_NODE *sat);

void sat2antenna(S_NODE *sat, struct ft_antenna *antenna);
UINT16 dvbs_calculate_progress(struct as_module_info *info,UINT32 freq, UINT8 polar, INT32 crnum);
#ifdef __cplusplus
}
#endif

#else /* FE_DVBS_SUPPORT */
#define dvbs_tp_duplicate_check(...)        (TRUE)
#define dvbs_tp_info2db(...)                    do{}while(0)
#define dvbs_convert_freq(...)                  do{}while(0)
#define dvbs_tp_sanity_check(...)           (TRUE)
#define sat2antenna(...)                        do{}while(0)
#define dvbs_calculate_progress(...)        (0)



#endif /*FE_DVBS_SUPPORT*/

#endif /* __AS_DVBS_H__ */

